package com.gaber.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author gaber
 * @date 2023/12/16 15:52:44
 * @description TODO
 *  * KEYIN, reducer阶段输入的key类型：Text
 *  * VALUEIN, reducer阶段输入的value类型：IntWritable
 *  * KEYOUT,  reducer阶段输入的key类型：Text
 *  * VALUEOUT reducer阶段输入的value类型：IntWritable
 */
public class WorkCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    private IntWritable outV = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        int sum = 0;
        // gaber, (1, 1)
        // gaber, (2)
        for (IntWritable value : values) {
            System.out.println(value.get() + "" + key);
            sum += value.get();
        }

        outV.set(sum);

        // 写出
        context.write(key, outV);
    }
}
